package net.hn.hnms.biz.emgcy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.hn.hnms.biz.emgcy.domain.EmgcyPlanPo;
import net.hn.hnms.biz.emgcy.domain.dto.EmgcyPlanDto;
import net.hn.hnms.biz.emgcy.domain.vo.EmgcyPlanVo;
import net.hn.hnms.biz.emgcy.service.EmgcyPlanService;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import net.hn.hnms.sys.file.api.RemoteFileService;
import net.hn.hnms.sys.system.api.RemoteDictService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: baijiajun
 * @Date: 2022/9/13
 */
@Api(value = "应急预案模块", tags = {"应急预案"})
@RestController
@RequestMapping("emgcyPlan")
public class EmgcyPlanController {
    @Autowired
    private EmgcyPlanService emgcyPlanService;
    @Autowired
    private RemoteDictService remoteDictService;
    @Autowired
    private RemoteFileService remoteFileService;


    @ApiOperation("应急预案信息表")
    @GetMapping("pageList")
    @AutoSetMineCode
    public R<ResponsePage<EmgcyPlanVo>> pageList(PageQuery pageQuery, EmgcyPlanDto emgcyPlanDto) {
        ResponsePage<EmgcyPlanVo> list=emgcyPlanService.pageList(pageQuery,emgcyPlanDto);
        return R.ok(list);
    }

    @ApiOperation("应急预案信息添加")
    @PostMapping("/add")
    public R add(@NotNull @RequestBody EmgcyPlanPo emgcyPlanPo){
        String str="";
        QueryWrapper<EmgcyPlanPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("event_type", emgcyPlanPo.getEventType());
        EmgcyPlanPo one = emgcyPlanService.getOne(queryWrapper);
        if(one!=null){
            String event_type = remoteDictService.selectDictLabel("event_type", one.getEventType());
            if(one.getPlanType()==1){
                str="总体预案";
            }else{
                str="专项预案";
            }
            throw new HuaNengException(500,"已有"+event_type+"对应的"+str);
        }
        Boolean flag = emgcyPlanService.add(emgcyPlanPo);
        if(flag){
            return R.ok("新增成功");
        }
        return R.fail("新增失败");
    }

    @ApiOperation("应急预案信息修改")
    @PutMapping("/update")
    public R update(@RequestBody EmgcyPlanPo emgcyPlanPo){
        Boolean flag = emgcyPlanService.update(emgcyPlanPo);
        if(flag){
            return R.ok("修改成功");
        }
        return R.fail("修改失败");
    }
    @ApiOperation("应急预案信息删除")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Long id){
        EmgcyPlanPo emgcyPlanPo = emgcyPlanService.getById(id);
        remoteFileService.remove(new Long[]{emgcyPlanPo.getInformationId().longValue()});
        Boolean flag = emgcyPlanService.delete(id);
        if(flag){
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }
}
